﻿@page "/posts/{id}"
@inject NotificationService NotificationService
@inject MessageService MessageService
@inherits BasePostPage
@using AcBlog.Client.UI.Components.Renderers

<Loader OnLoad="@OnLoad" OnLoaded="@OnLoaded" LoadKey="@Id" @ref="loader">
    <PageContainer Title="@Post.Title" PageTitle="@Title">
        <Breadcrumb>
            <Breadcrumb>
                <BreadcrumbItem>
                    <a href="">
                        <MdiIcon Type="@IconNames.Home" />
                        <span>Home</span>
                    </a>
                </BreadcrumbItem>
                <BreadcrumbItem>
                    <a href="@UrlGenerator.Posts()">
                        <MdiIcon Type="@IconNames.Posts" />
                        <span>Posts</span>
                    </a>
                </BreadcrumbItem>
                <BreadcrumbItem>
                    <span>@Post.Id</span>
                </BreadcrumbItem>
            </Breadcrumb>
        </Breadcrumb>
        <Extra>
            @if (IsProtected)
            {
                <button class="btn btn-success mdi mdi-shield-key-outline" @onclick="e => inputPasswordModalVisible = true">Deprotect</button>
            }
            <AuthorizeView>
                <Authorized>
                    <ButtonLink Href="@($"posts/{Uri.EscapeDataString(Id)}/edit")">
                        <MdiIcon Type="@IconNames.Edit"></MdiIcon>
                        <span>Edit</span>
                    </ButtonLink>
                    <Popconfirm Title="@("Are you sure delete this post?")" OnConfirm="OnDelete" Placement="@PlacementType.BottomRight">
                        <Button Danger>
                            <MdiIcon Type="@IconNames.Delete"></MdiIcon>
                            <span>Delete</span>
                        </Button>
                    </Popconfirm>
                </Authorized>
            </AuthorizeView>
        </Extra>
        <Tags>
            <PostMetadataDisplay Value="@Post" />
        </Tags>
        <ChildContent>
            @if (IsProtected)
            {
                <Modal Title="@("Input Password")" Visible="inputPasswordModalVisible" OnOk="e=>OnPasswordSubmit()" OnCancel="e => inputPasswordModalVisible = false">
                    <InputPassword @bind-Value="@Password" Size="@InputSize.Large" OnPressEnter="e=>OnPasswordSubmit()" />
                </Modal>
            }
            else
            {
                @switch (Post.Type)
                {
                    case PostType.Article:
                    case PostType.Note:
                        <ArticleRenderer Document="@Post.Content"></ArticleRenderer>
                        break;
                    case PostType.Slides:
                        <SlidesRenderer Document="@Post.Content" Style="@SlidesStyle"></SlidesRenderer>
                        <button type="button" class="btn btn-primary mt-2" style="width: 100%;" @onclick="@OnFullViewClick">Full View</button>
                        break;
                    default:
                        break;
                }

                <div class="mt-5">
                    <AcBlog.Client.UI.Pages.Comments.Components.CommentList Uri="@UrlGenerator.Post(Post)"></AcBlog.Client.UI.Pages.Comments.Components.CommentList>
                </div>
            }
        </ChildContent>
    </PageContainer>
</Loader>

@code {
    [Parameter]
    public string Id { get; set; }

    private Post Post { get; set; }

    private bool IsProtected { get; set; } = false;

    private bool inputPasswordModalVisible = false;

    private string Password { get; set; } = string.Empty;

    private string SlidesStyle { get; set; } = "border: 0; height: 500px; width: 100%";

    private Loader loader;

    private async Task OnLoaded()
    {
        if (Post is null)
        {
            Title = "Loading...";
        }
        else
        {
            if (IsProtected)
            {
                Title = "Protected Post";
                inputPasswordModalVisible = true;
            }
            else
            {
                Title = Post.Title;
            }
            await Service.StatisticService?.Visited(UrlGenerator, Post);
        }
    }

    async Task OnDelete()
    {
        var pass = await Service.PostService.Delete(Post.Id);
        if (pass)
        {
            _ = MessageService.Success("Deleted post successfully.");
            NavigationManager.NavigateTo($"posts");
        }
        else
        {
            _ = MessageService.Error("Failed to delete post.");
        }
    }

    private async Task OnLoad()
    {
        Post = await Service.PostService.Get(Id);
        IsProtected = await Service.PostService.Protector.IsProtected(Post.Content);
    }

    private async Task OnPasswordSubmit()
    {
        try
        {
            Document dep = await Service.PostService.Protector.Deprotect(Post.Content, new Data.Protections.ProtectionKey
            {
                Password = Password
            });
            Post.Content = dep;
            IsProtected = await Service.PostService.Protector.IsProtected(Post.Content);
            if (!IsProtected)
                inputPasswordModalVisible = false;
        }
        catch
        {
            inputPasswordModalVisible = true;

            await NotificationService.Error(new NotificationConfig
            {
                Message = "Wrong password",
            });
        }
    }

    private Task OnFullViewClick()
    {
        SlidesStyle = "left: 0; right: 0; top: 0; bottom: 0; margin:0; border:0; position: absolute; height: 100%; width:100%; z-index: 500;";
        return Task.CompletedTask;
    }
}
